package com.mindcraft.shop.service.Impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mindcraft.base.model.MindCraftResult;
import com.mindcraft.base.utils.CommonConst;
import com.mindcraft.shop.client.UserClient;
import com.mindcraft.shop.mapper.CommentMapper;
import com.mindcraft.shop.po.CommunityComment;
import com.mindcraft.shop.service.CommentService;
import com.mindcraft.shop.vo.CommentRequestVo;
import com.mindcraft.shop.vo.CommentVo;
import com.mindcraft.user.po.McUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, CommunityComment> implements CommentService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserClient userClient;


    @Override
    public MindCraftResult<CommentRequestVo> listComment(CommentRequestVo commentRequestVo) {
        if (commentRequestVo.getFloorCommentId() == null) {
            lambdaQuery().eq(CommunityComment::getCommunityId, commentRequestVo.getCommunityId())
                    .eq(CommunityComment::getParentCommentId, CommonConst.FIRST_COMMENT)
                    .orderByDesc(CommunityComment::getLikeCount)
                    .page(commentRequestVo);
            //一级评论
            List<CommunityComment> comments = commentRequestVo.getRecords();
            if (CollectionUtils.isEmpty(comments)) {
                return MindCraftResult.success(commentRequestVo);
            }
            //查询二级评论
            List<CommentVo> commentVOs = comments.stream().map(c -> {
                CommentVo commentVO = buildCommentVO(c);


                Page page = new Page(1, 2);
                //如果floorid与一级评论id相等，则此二级评论就是一级评论的子评论
                lambdaQuery()
                        .eq(CommunityComment::getCommunityId, commentRequestVo.getCommunityId())
                        .eq(CommunityComment::getFloorCommentId, c.getId())
                        .orderByDesc(CommunityComment::getLikeCount)
                        .page(page);

                List<CommunityComment> childComments = page.getRecords();

                if (childComments != null) {
                    long allReply = page.getTotal();
                    commentVO.setAllReply(allReply);

                    List<CommentVo> ccVO = childComments.stream().map(cc ->
                            buildCommentVO(cc))
                            .collect(Collectors.toList());
                    page.setRecords(ccVO);
                }
                commentVO.setChildComments(page);
                return commentVO;
            }).collect(Collectors.toList());
            commentRequestVo.setRecords(commentVOs);
        } else {
            //查看更多子评论
            lambdaQuery()
                    .eq(CommunityComment::getCommunityId, commentRequestVo.getCommunityId())
                    .eq(CommunityComment::getFloorCommentId, commentRequestVo.getFloorCommentId())
                    .orderByDesc(CommunityComment::getLikeCount)
                    .page(commentRequestVo);
            List<CommunityComment> childComments = commentRequestVo.getRecords();
            if (CollectionUtils.isEmpty(childComments)) {
                return MindCraftResult.success(commentRequestVo);
            }
            List<CommentVo> ccVO = childComments.stream().map
                    (cc -> buildCommentVO(cc))
                    .collect(Collectors.toList());
            commentRequestVo.setRecords(ccVO);
        }

        return MindCraftResult.success(commentRequestVo);
    }


    private CommentVo buildCommentVO(CommunityComment c) {

        CommentVo commentVO = new CommentVo();
        BeanUtils.copyProperties(c, commentVO);

        McUser user = new McUser();

        if(redisTemplate.hasKey(CommonConst.USER_CACHE +commentVO.getUserId())){
            user = (McUser) redisTemplate.opsForValue().get(CommonConst.USER_CACHE +commentVO.getUserId());
        }else{

            user = userClient.getUser(commentVO.getUserId());
        }

        if (user != null) {
            commentVO.setAvatar(user.getUserpic());
            commentVO.setUsername(user.getUsername());
        }

//        if (!StringUtils.hasText(commentVO.getUsername())) {
//            commentVO.setUsername(MindCraftUtil.getRandomName(commentVO.getUserId().toString()));
//        }

        if (commentVO.getParentUserId() != null) {
            McUser u = new McUser();

            if(redisTemplate.hasKey(CommonConst.USER_CACHE +commentVO.getParentUserId())){
                u = (McUser) redisTemplate.opsForValue().get(CommonConst.USER_CACHE +commentVO.getParentUserId());
            }else{
                u = userClient.getUser(commentVO.getParentUserId());
            }


            if (u != null) {
                commentVO.setParentUsername(u.getUsername());
            }


//            if (!StringUtils.hasText(commentVO.getParentUsername())) {
//                commentVO.setParentUsername(MindCraftUtil.getRandomName(commentVO.getParentUserId().toString()));
//            }
        }
        return commentVO;
    }
}
